// encoding is UTF-8
#include <stdio.h>
// number of candidates
#define N 3
// number of voters
#define VOTERS 100

// the struct for the candidate
typedef struct Candidate
{
    // Note that This is a INT !
    int code;
    int score;
} candidate;

// we declare the candidate array here, and it is static
// so that it can be accessed in the whole program
candidate can[N][VOTERS];
int voters = 0;

// input all candidates
void input()
{
    printf("How many \033[32mpeople\033[0m are voting to the candidate: ");
    scanf("%d", &voters);
    // clear the candidate, to avoid unexpected result
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < voters; j++)
        {
            // the default code
            can[i][j].code = j + 1;
            can[i][j].score = 0;
        }
    }
    // input the data
    for (int i = 0; i < voters; i++)
    {
        int tmp[3];
        printf("Vote for candidate\033[32m %d \033[0m, format is 'a b c': ", i + 1);
        scanf("%d %d %d", &tmp[0], &tmp[1], &tmp[2]);
        if (tmp[0] == tmp[1] && tmp[1] == tmp[2] || tmp[0] == tmp[2])
        {
            printf("\033[31mIllegal input\033[0m\n");
        }
        else
        {
            // update the candidate
            can[tmp[0] - 1][i].score += 5;
            can[tmp[1] - 1][i].score += 3;
            can[tmp[2] - 1][i].score += 2;
        }
    }
}

// analyze the data
void analyze()
{
    if (voters == 0)
    {
        printf("Voting is required before analyze the data!\n");
        return;
    }
    int all_total_scores_for_sort[N];
    for (int i = 0; i < N; i++)
    {
        all_total_scores_for_sort[i] = 0;
    }
    // check the candidate
    for (int i = 0; i < N; i++)
    {

        int total_scores = 0;

        printf("\033[33mCandidate %d Scores are:\033[0m\t", can[i][0].code);
        for (int j = 0; j < voters; j++)
        {
            total_scores += can[i][j].score;
            printf("%d\t", can[i][j].score);
        }
        // total score for this candidate
        printf("Total %d\n", total_scores);
        all_total_scores_for_sort[i] = total_scores;
        total_scores = 0;
    }

    // sort the all_total_scores_for_sort
    // This is for 3 People
    for (int i = 0; i < N; i++)
    {
        // this is bubble sort
        for (int j = 0; j < N - 1 - i; j++)
        {
            // swap the items
            if (all_total_scores_for_sort[j] < all_total_scores_for_sort[j + 1])
            {
                // Here we sort a stand-alone list
                // but we can also use this index to sort candidates

                // swap j and j+1 list item
                int tmp = all_total_scores_for_sort[j];
                all_total_scores_for_sort[j] = all_total_scores_for_sort[j + 1];
                all_total_scores_for_sort[j + 1] = tmp;
                // the index is the first index
                // we swap the entire candidates using this first index

                // swap candidate lines, line j and j+1
                // Notice that candidates are always N

                // Doing this can broadcast the previous sort result
                // To the entire candidates
                for (int column = 0; column < N; column++)
                {
                    int tmp = can[j][column].score;
                    can[j][column].score = can[j + 1][column].score;
                    can[j + 1][column].score = tmp;
                }
            }
        }
    }
    printf("\n");
    printf("\033[32mSorted total scores are %d %d %d\033[0m\n",
           all_total_scores_for_sort[0],
           all_total_scores_for_sort[1],
           all_total_scores_for_sort[2]);
    printf("The table is:\n");
    // check the candidate
    for (int i = 0; i < N; i++)
    {
        int total_scores = 0;
        printf("\033[33mCandidate %d Scores are:\033[0m\t", can[i][0].code);
        for (int j = 0; j < voters; j++)
        {
            total_scores += can[i][j].score;
            printf("%d\t", can[i][j].score);
        }
        // total score for this candidate
        printf("Total %d\n", total_scores);
        all_total_scores_for_sort[i] = total_scores;
        total_scores = 0;
    }
}

int main(void)
{
    while (1)
    {
        int usr_input;
        printf("0: input  1: analyze  2: quit --> ");
        scanf("%d", &usr_input);
        switch (usr_input)
        {
        case 0:
            input();
            continue;
        case 1:
            analyze();
            continue;
        case 2:
            return 0;
        default:
            printf("Cannot understand input\n");
            continue;
        }
    }
    return 0;
}
